package com.zyd.module.api.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zyd.common.util.Pagination;
import com.zyd.common.util.R;
import com.zyd.module.admin.model.BizArticle;
import com.zyd.module.admin.service.BizArticleService;
import com.zyd.module.admin.vo.ArticleConditionVo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@Slf4j
@CrossOrigin
@Api(tags = "主页")
@RestController
@RequestMapping("/index/article")
public class IndexArticleController {


    @Autowired
    public BizArticleService articleService;


    @PostMapping("/articleList")
    public R getArticleList(@RequestBody Map map){
        System.out.println(map);
        Integer pageNumber = (Integer) map.get("pageNumber");
        Integer pageSize = (Integer) map.get("pageSize");

        IPage<BizArticle> page = new Pagination<>(pageNumber, pageSize);
        ArticleConditionVo articleConditionVo = new ArticleConditionVo();

        List<BizArticle> articleList = articleService.findByCondition(page, articleConditionVo);
        long total = page.getTotal();
        map.clear();
        map.put("articleList",articleList);
        map.put("totalPage",page.getTotal());
        return R.ok().data("productMap",map);
    }

    @PostMapping("/getArticleById")
    public R getArticleById(@RequestBody Map map){
        Integer articleId = Integer.valueOf((String) map.get("articleId"));
        BizArticle article = articleService.selectById(articleId);
        return R.ok().data("article",article);
    }

}
